//
//  NewFeatureViewController.swift
//  新浪微博
//
//  Created by 李赛 on 15/5/14.
//  Copyright (c) 2015年 李赛. All rights reserved.
//

import UIKit

class NewFeatureViewController: UICollectionViewController {
    @IBOutlet weak var layout: UICollectionViewFlowLayout!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        layout.itemSize = view.bounds.size
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource
    
    let imgCount = 4

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return imgCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FeatureCell", forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        cell.imgIndex = indexPath.item
    
        return cell
    }

    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last as! NSIndexPath
        
        if path.item == imgCount - 1{
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            cell.showStartButton()
        }
        
    }
}

class NewFeatureCell: UICollectionViewCell {
    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var startButton: UIButton!
    
    var imgIndex : Int = 0 {
        didSet{
            self.iconView.image = UIImage(named: "new_feature_\(imgIndex + 1)")
            self.startButton.hidden = true
        }
    }
    
    func showStartButton(){
        
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        // 添加动画属性
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5.0, options: nil, animations: {
            self.startButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
        }) {_ in}
    }
    
    @IBAction func startButtonClick(sender: AnyObject) {
     
//        println("进入主页面")
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: "Main")
    }
    
}
